package com.pickgliss.ui.core
{
	import com.pickgliss.toplevel.StageReferance;
	import com.pickgliss.ui.LayerManager;
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class SpriteLayer extends Sprite
	{
		public function SpriteLayer (mouseEnable : Boolean = false )
		{
			init(mouseEnable);
			super();
		}
		
		private function init(mouseEnable : Boolean):void
		{
			_blackGoundList = new Vector.<DisplayObject>();
			_alphaGoundList = new Vector.<DisplayObject>();
			mouseEnabled = mouseEnable;
			
			StageReferance.stage.addEventListener(Event.RESIZE, __onResize);
			__onResize()
		}
		
		protected function __onResize(event:Event=null):void
		{
			for (var i:int = 0; i < this.numChildren; i++) 
			{
				var dis:DisplayObject=this.getChildAt(i);
				if(LayerManager.Instance.settingMap[dis])
				{
					var sw:Number=StageReferance.stageWidth;
					var sh:Number=StageReferance.stageHeight;
					if(sw>1500)sw=1500;
					if(sh>900)sh=900;
					dis.x = (sw - ObjectUtils.getObjectWidth(dis))/2;
					dis.y = (sh - ObjectUtils.getObjectHeight(dis))/2;
				}
			}
		}
		
		private var _blackGoundList:Vector.<DisplayObject>;
		private var _alphaGoundList:Vector.<DisplayObject>;
		private var _blackGound:Sprite;
		public function addTolayer(child:DisplayObject,blockGound:int,containerType:int = 0):void
		{
			if(blockGound == LayerManager.BLCAK_BLOCKGOUND)
			{
				if(_blackGoundList.indexOf(child) != -1)
				{
					_blackGoundList.splice(_blackGoundList.indexOf(child),1);
				}
				_blackGoundList.push(child);
			}else if(blockGound == LayerManager.ALPHA_BLOCKGOUND)
			{
				if(_alphaGoundList.indexOf(child) != -1)
				{
					_alphaGoundList.splice(_alphaGoundList.indexOf(child),1);
				}
				_alphaGoundList.push(child);
			}
			child.addEventListener(Event.REMOVED_FROM_STAGE,__onChildRemoveFromStage);
			if(_autoClickTotop)child.addEventListener(MouseEvent.MOUSE_DOWN,__onClickToTop);
			addToContainer(child,containerType);
			arrangeBlockGound();
		}
		protected function addToContainer(child:DisplayObject,container:int = 0):void
		{
			addChild(child);
		}
		private function __onClickToTop(event:MouseEvent):void
		{
			var child:DisplayObject = event.currentTarget as DisplayObject;
			addToContainer(child);
		}

		private function __onChildRemoveFromStage(event:Event):void
		{
			var child:DisplayObject = event.currentTarget as DisplayObject;
			child.removeEventListener(Event.REMOVED_FROM_STAGE,__onChildRemoveFromStage);
			child.removeEventListener(MouseEvent.MOUSE_DOWN,__onClickToTop);
			if(_blackGoundList.indexOf(child)!=-1)_blackGoundList.splice(_blackGoundList.indexOf(child),1);
			if(_alphaGoundList.indexOf(child)!=-1)_alphaGoundList.splice(_alphaGoundList.indexOf(child),1);
			delete LayerManager.Instance.settingMap[child];
			arrangeBlockGound();
		}
		
		private function arrangeBlockGound():void
		{
			if(_blackGound && _blackGound.parent)_blackGound.parent.removeChild(_blackGound);
			if(_alphaGound && _alphaGound.parent)_alphaGound.parent.removeChild(_alphaGound);
			var child:DisplayObject;
			var childIndex:int;
			if(_blackGoundList.length > 0)
			{
				child = _blackGoundList[_blackGoundList.length-1];
				childIndex = getChildIndex(child);
				addChildAt(blackGound,childIndex);
			}
			else
			{
				removeBlackGound();
			}
			
			if(_alphaGoundList.length > 0)
			{
				child = _alphaGoundList[_alphaGoundList.length-1];
				childIndex = getChildIndex(child);
				addChildAt(alphaGound,childIndex);
			}
			else
			{
				removeAlphaGound();
			}
		}
		
		private function get blackGound():Sprite
		{
			if(_blackGound == null)
			{
				_blackGound = new Sprite();
				_blackGound.graphics.beginFill(0x000000,0.6);
				_blackGound.graphics.drawRect(-500,-200,2100,1600);
				_blackGound.graphics.endFill();
				_blackGound.addEventListener(MouseEvent.MOUSE_DOWN,__onBlackGoundMouseDown);
			}
			return _blackGound;
		}
		private function removeBlackGound() : void
		{
			if(_blackGound)
			{
				_blackGound.graphics.clear();
				_blackGound.removeEventListener(MouseEvent.MOUSE_DOWN,__onBlackGoundMouseDown);
				if(_blackGound.parent)_blackGound.parent.removeChild(_blackGound);_blackGound = null;
			}
		}

		private function __onBlackGoundMouseDown(event:MouseEvent):void
		{
			StageReferance.stage.focus = _blackGoundList[_blackGoundList.length-1] as InteractiveObject;
			event.stopImmediatePropagation();
			event.stopPropagation();
		}
		
		private var _alphaGound:Sprite;
		private function get alphaGound():Sprite
		{
			if(_alphaGound == null)
			{
				_alphaGound = new Sprite();
				_alphaGound.graphics.beginFill(0x000000,0.001);
				_alphaGound.graphics.drawRect(-500,-200,2100,1110);
				_alphaGound.graphics.endFill();
				_alphaGound.addEventListener(MouseEvent.MOUSE_DOWN,__onAlphaGoundDownClicked);
				_alphaGound.addEventListener(MouseEvent.MOUSE_UP,__onAlphaGoundUpClicked);
			}
			return _alphaGound;
		}
		
		private function removeAlphaGound() : void
		{
			if(_alphaGound)
			{
				_alphaGound.graphics.clear();
				_alphaGound.removeEventListener(MouseEvent.MOUSE_DOWN,__onAlphaGoundDownClicked);
				_alphaGound.removeEventListener(MouseEvent.MOUSE_UP,__onAlphaGoundUpClicked);
				if(_alphaGound.parent)_alphaGound.parent.removeChild(_alphaGound);
				_alphaGound = null;
			}
		}

		private function __onAlphaGoundDownClicked(event:MouseEvent):void
		{
			var target:DisplayObject = _alphaGoundList[_alphaGoundList.length - 1];
//			target.filters = ComponentFactory.Instance.creatFilters(ComponentSetting.ALPHA_LAYER_FILTER);
			StageReferance.stage.focus = target as InteractiveObject;
		}
		
		private function __onAlphaGoundUpClicked(event:MouseEvent):void
		{
			var target:DisplayObject = _alphaGoundList[_alphaGoundList.length - 1];
			target.filters = null;
		}
		
		private var _autoClickTotop:Boolean;
		public function set autoClickTotop(value:Boolean):void
		{
			if(_autoClickTotop == value)return;
			_autoClickTotop = value;
		}
		
		
	}
}